﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.IO;

namespace Mewdam.OAuth
{
    public class WebRequestBuilder
    {
        HttpWebRequest request;

        public WebRequestBuilder(OAuthBase.HttpMethod httpMethod,string urlString ,string queryString) 
        {
            
            request = (HttpWebRequest)WebRequest.Create(urlString+"?"+queryString);
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            request.Method = httpMethod.ToString();
            request.ContentType = "application/x-www-form-urlencoded";
            request.ServicePoint.Expect100Continue = false;


        }

        public string GetResponse()
        {
            string responseBody;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream());
                responseBody = sr.ReadToEnd();
                return responseBody;
            }
            catch (WebException ex)
            {
                //Debug.Print(ex.Message);
                throw ex;
            }
            //return null;
            finally 
            {
                
            }

            //return null;
        }


        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }
}